﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class ES_Employees : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private ICommandDetailBLL cmdBLL = new CommandDetailBLL();
        private IHisEducationBLL eduBLL = new HisEducationBLL();
        private IRoyalItemHistoriesBLL royBLL = new RoyalItemHistoriesBLL();
        private ISpecialSkillBLL skillBLL = new SpecialSkillBLL();
        private IAbsenceBLL abBLL = new AbsenceBLL();
        private IHisSpouseBLL spouBLL = new HisSpouseBLL();
        private ITrainingBLL trBLL = new TrainingBLL();
        private ILegateeBLL lgBLL = new LegateeBLL();
        private IIncreaseBLL icBLL = new IncreaseBLL();
        private IHisGoodNessBLL gnBLL = new HisGoodNessBLL();
        private IHisNameBLL nameBLL = new HisNameBLL();
        private PayrollsBLL payBLL = new PayrollsBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            string strQueryString = Request.QueryString["Sequence"].ToString();

            if (strQueryString == "ESS")
            {
	            Session["emp_seq"] = ssMng.EmpSeq; // Session["EmpSeq"];
            }
            else
                Session["emp_seq"] = Request.QueryString["Sequence"].ToString();            

            if (!IsPostBack)
            {
                bindYearCombo();
                AddTab(0);
                AddPageView(RadTabStrip1.FindTabByText("ข้อมูลเบื้องต้น"));
                AddTab(1);
                AddTab(2);
                AddTab(3);
                AddTab(4);
                AddTab(5);
                AddTab(6);
                AddTab(7);
                AddTab(8);
                AddTab(9);
                AddTab(10);
                AddTab(11);
                AddTab(12);
                AddTab(13);
                AddTab(14);
            }
        }
        
        public void bindYearCombo()
        {
            int curYear = int.Parse(DateTime.Today.Year.ToString());
            curYear += 543;
            int MinYear = curYear - 10;
            int MaxYear = curYear + 10;
            DataTable dt = new DataTable();
            DataColumn dc = dt.Columns.Add("Name");

            for (int i = MinYear; i <= MaxYear; i++)
            {
                dt.Rows.Add(Convert.ToString(i));
            }
            cmbYear.DataSource = dt;
            cmbYear.DataBind();
            cmbYear.SelectedValue = curYear.ToString();
        }

        private void AddTab(int tabIndex)
        {
            RadTab tab = new RadTab();

            switch (tabIndex)
            {
                case 0:
                    tab.Value = "PS_HisPrivate";
                    tab.Text = "ข้อมูลเบื้องต้น";
                    break;
                case 1:
                    tab.Value = "PS_HisEducations";
                    tab.Text = "ประวัติการศึกษา";
                    break;
                case 2:
                    tab.Value = "PS_Childrens";
                    tab.Text = "ข้อมูลบุตร";
                    break;
                case 3:
                    tab.Value = "PS_Parents";
                    tab.Text = "ข้อมูลบิดามารดา";
                    break;
                case 4:
                    tab.Value = "PS_HisSpouses";
                    tab.Text = "ข้อมูลคู่สมรส";
                    break;
                case 5:
                    tab.Value = "PS_HisNames";
                    tab.Text = "ประวัติการเปลี่ยนแปลงชื่อ-สกุล";
                    break;
                case 6:
                    tab.Value = "PS_Contacts";
                    tab.Text = "ข้อมูลบุคคลที่ติดต่อ";
                    break;
                case 7:
                    tab.Value = "PS_SpecialSkills";
                    tab.Text = "ทักษะความสามารถพิเศษ";
                    break;
                case 8:
                    tab.Value = "PS_HisPosition";
                    tab.Text = "ประวัติการรับราชการ";
                    break;
                case 9:
                    tab.Value = "PS_HisSalary";
                    tab.Text = "ประวัติการได้รับเงินเดือน";
                    break;
                case 10:
                    tab.Value = "PS_HisOversea";
                    tab.Text = "ประวัติการปฏิบัติราชการต่างประเทศ";
                    break;
                case 11:
                    tab.Value = "PS_HisRoyalItems";
                    tab.Text = "ประวัติการได้รับพระราชทานเครื่องราชฯ";
                    break;
                case 12:
                    tab.Value = "PS_HisTraining";
                    tab.Text = "ประวัติการฝึกอบรม/สัมมนา";
                    break;
                case 13:
                    tab.Value = "PS_HisAbsence";
                    tab.Text = "ประวัติการลา";
                    break;
                case 14:
                    tab.Value = "PS_HisEnglistTest";
                    tab.Text = "ประวัติการทดสอบภาษาอังกฤษ";
                    break;
            }
            RadTabStrip1.Tabs.Add(tab);
        }

        protected void RadMultiPage1_PageViewCreated(object sender, RadMultiPageEventArgs e)
        {
            //string userControlName = e.PageView.ID + "Info.ascx";
            string userControlName = "../UserControls/" + e.PageView.ID + "Info.ascx";

            Control userControl = Page.LoadControl(userControlName);
            userControl.ID = e.PageView.ID + "_userControl";

            e.PageView.Controls.Add(userControl);
        }

        private void AddPageView(RadTab tab)
        {
            RadPageView pageView = new RadPageView();
            pageView.ID = tab.Value;
            RadMultiPage1.PageViews.Add(pageView);
            //pageView.CssClass = "pageView";
            tab.PageViewID = pageView.ID;
        }

        protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
        {
            AddPageView(e.Tab);
            e.Tab.PageView.Selected = true;
        }

        protected void cmdPrint_Click(object sender, EventArgs e)
        {
            string strReportName = cmbReportName.SelectedValue;
            string strEmpSequence = Session["emp_seq"].ToString();
            DataSet ds = null;
            DataTable dt = null;
            DataSet[] dsSubReport;
            DataTable[] dtSubReport;
            string[] strSubReportName;

            switch (strReportName)
            {
                case "RPT_PS_EmployeeDtls":
                    strSubReportName = new string[14];
                    dsSubReport = new DataSet[14];
                    dtSubReport = new DataTable[14];

                    ds = empBLL.LoadEmployees(Convert.ToInt32(strEmpSequence), "", true, "");
                    if (ds.Tables.Count > 0)
                        dt = ds.Tables[0].Copy();

                    //ประวัติการรับราชการ                    
                    strSubReportName[0] = "RPT_PS_HisPositions";
                    dsSubReport[0] = cmdBLL.LoadCommandDetails(Convert.ToInt32(strEmpSequence), SystemInfo._HisPosition);
                    if (dsSubReport[0].Tables.Count > 0)
                        dtSubReport[0] = dsSubReport[0].Tables[0].Copy();

                    //ประวัติการเลื่อนเงินเดือน         
                    strSubReportName[1] = "RPT_PS_HisSalary";
                    dsSubReport[1] = cmdBLL.LoadCommandDetails(Convert.ToInt32(strEmpSequence), SystemInfo._HisSalary);
                    if (dsSubReport[1].Tables.Count > 0)
                        dtSubReport[1] = dsSubReport[1].Tables[0].Copy();

                    //ประวัติการรับเงินเพิ่มพิเศษ   
                    strSubReportName[2] = "RPT_PS_HisSalaryExtra";
                    dsSubReport[2] = cmdBLL.LoadCommandDetails(Convert.ToInt32(strEmpSequence), SystemInfo._HisSalary);
                    if (dsSubReport[2].Tables.Count > 0)
                        dtSubReport[2] = dsSubReport[2].Tables[0].Copy();

                    //ประวัติการศึกษา      
                    strSubReportName[3] = "RPT_PS_HisEducate";
                    dsSubReport[3] = eduBLL.LoadEducationHistories((int?)null, strEmpSequence);
                    if (dsSubReport[3].Tables.Count > 0)
                        dtSubReport[3] = dsSubReport[3].Tables[0].Copy();

                    //ประวัติการฝึกอบรม               
                    strSubReportName[4] = "RPT_PS_HisTraining";
                    dsSubReport[4] = trBLL.LoadTrainingHistories(Int32.Parse(strEmpSequence));
                    if (dsSubReport[4].Tables.Count > 0)
                        dtSubReport[4] = dsSubReport[4].Tables[0].Copy();

                    //ความสามารถพิเศษ  
                    strSubReportName[5] = "RPT_PS_SpecialSkill";
                    dsSubReport[5] = skillBLL.LoadSpecialSkill((int?)null, Int32.Parse(strEmpSequence));
                    if (dsSubReport[5].Tables.Count > 0)
                        dtSubReport[5] = dsSubReport[5].Tables[0].Copy();

                    //ข้อมูลทายาท  
                    strSubReportName[6] = "RPT_PS_Legatees";
                    dsSubReport[6] = lgBLL.LoadLegatee((int?)null, Int32.Parse(strEmpSequence));
                    if (dsSubReport[6].Tables.Count > 0)
                        dtSubReport[6] = dsSubReport[6].Tables[0].Copy();

                    //ประวัติการลา สาย ขาด  
                    strSubReportName[7] = "RPT_PS_HisAbsences";
                    dsSubReport[7] = abBLL.LoadAbsences((int?)null, Int32.Parse(strEmpSequence));
                    if (dsSubReport[7].Tables.Count > 0)
                        dtSubReport[7] = dsSubReport[7].Tables[0].Copy();

                    //ประวัติทางวินัย
                    strSubReportName[8] = "RPT_PS_HisDiscipline";
                    dsSubReport[8] = cmdBLL.LoadCommandDetails(Convert.ToInt32(strEmpSequence), SystemInfo._HisDiscipline);
                    if (dsSubReport[8].Tables.Count > 0)
                        dtSubReport[8] = dsSubReport[8].Tables[0].Copy();

                    //ประวัติเวลาทวีคูณ  
                    strSubReportName[9] = "RPT_PS_HisIncrease";
                    dsSubReport[9] = icBLL.LoadHisIncrease((int?)null, Int32.Parse(strEmpSequence));
                    if (dsSubReport[9].Tables.Count > 0)
                        dtSubReport[9] = dsSubReport[9].Tables[0].Copy();

                    //ประวัติการรับความดีความชอบ  
                    strSubReportName[10] = "RPT_PS_HisGoodNess";
                    dsSubReport[10] = gnBLL.LoadHisGoodNess((int?)null, Int32.Parse(strEmpSequence));
                    if (dsSubReport[10].Tables.Count > 0)
                        dtSubReport[10] = dsSubReport[10].Tables[0].Copy();

                    //ประวัติการสมรส  
                    strSubReportName[11] = "RPT_PS_HisSpouses";
                    dsSubReport[11] = spouBLL.LoadSpouse((int?)null, Int32.Parse(strEmpSequence));
                    if (dsSubReport[11].Tables.Count > 0)
                        dtSubReport[11] = dsSubReport[11].Tables[0].Copy();

                    //ประวัติการเปลี่ยนแปลงชื่อ - สกุล  
                    strSubReportName[12] = "RPT_PS_HisName";
                    dsSubReport[12] = nameBLL.LoadHisNames((int?)null, Int32.Parse(strEmpSequence));
                    if (dsSubReport[12].Tables.Count > 0)
                        dtSubReport[12] = dsSubReport[12].Tables[0].Copy();

                    //ประวัติการรับเครื่องราช
                    strSubReportName[13] = "RPT_PS_HisRoyalItems";
                    dsSubReport[13] = royBLL.LoadRoyalItemHistories((int?)null, Int32.Parse(strEmpSequence), (int?)null);
                    if (dsSubReport[13].Tables.Count > 0)
                        dtSubReport[13] = dsSubReport[13].Tables[0].Copy();

                    //ประวัติราชการพิเศษ
                    //strSubReportName[14] = "RPT_PS_HisSpecialCommands";
                    //dsSubReport[14] = skillBLL.LoadSpecialSkill((int?)null, Int32.Parse(strEmpSequence));
                    //if (dsSubReport[14].Tables.Count > 0)
                    //    dtSubReport[14] = dsSubReport[14].Tables[0].Copy();

                    //ความเชี่ยวชาญพิเศษ
                    //strSubReportName[15] = "RPT_PS_SpecialExperts";
                    //dsSubReport[15] = skillBLL.LoadSpecialSkill((int?)null, Int32.Parse(strEmpSequence));
                    //if (dsSubReport[15].Tables.Count > 0)
                    //    dtSubReport[15] = dsSubReport[15].Tables[0].Copy();

                    Session["strSubReportName"] = strSubReportName;
                    Session["dtSubReport"] = dtSubReport;
                    break;
                case "RPT_PS_PayrollSlips":
                    dt = payBLL.RPT_PayrollSlips(Int32.Parse(Session["emp_seq"].ToString()), cmbMonth.SelectedValue, cmbYear.SelectedValue);
                    break;
                    
            }
            Session["dtReport"] = dt;
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "onclick", "openReport('" + strReportName + "')", true);            
        }

       
    }
}
